//
//  WelcomeViewController.swift
//  01MicroBlog
//
//  Created by Apple on 15/7/31.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    private var bottomConstraint:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 准备UI操作
        prepareForUI()
        
        // 加载用户头像
        if let iconUrl = UserAccount.sharedAccount!.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: iconUrl))
        }

    }
    
    // 更改底部约束，设置动画效果
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        
        // 提示：修改约束不会立即生效，添加了一个标记，统一由自动布局系统更新约束
        bottomConstraint?.constant = UIScreen.mainScreen().bounds.height - bottomConstraint!.constant
        
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            // 让约束立即生效
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                // 发送通知，切换控制器
                NSNotificationCenter.defaultCenter().postNotificationName(WJRootViewControllerSwitchNotification, object: true)
        }
    }

    private func prepareForUI() {
        
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        
        // 自动布局
        // 1> 背景图片
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": backImageView]))
        
        // 2> 头像
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 160))
        // 记录底边约束
        bottomConstraint = view.constraints.last
        
        
        // 3> 标签
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))

        
    }
    
    // 背景图片
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    // 头像
    lazy var iconView:UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.masksToBounds = true
        iconView.layer.cornerRadius = 45
        return iconView
    }()
    
    // 文字
    /// 消息文字
    private lazy var label: UILabel = {
        let label = UILabel()
        
        label.text = "王者降临"
        label.sizeToFit()
        
        return label
    }()
    
}
